﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms
{
    public partial class frmStartTime : Form
    {
        private DisciplineListEntity discipline;
        private DisciplineListTableAdapter taDisciplineList = new DisciplineListTableAdapter();
        
        // Constructor
        public frmStartTime(DisciplineListEntity discipline)
        {
            InitializeComponent();

            this.discipline = discipline;
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form load
        private void frmStartTime_Load(object sender, EventArgs e)
        {
            try
            {
                // Title
                this.lblTitle.Text = this.discipline.Name;

                // Status
                this.disciplinesStatusesTableAdapter.Fill(this.aDbDataSet.DisciplinesStatuses);
                this.disciplinesStatusesComboBox.SelectedValue = this.discipline.Status;

                // Start time
                this.dtpStartTime.Value = this.discipline.StartTime;

                // Real start time
                if (this.discipline.RealStartTime.HasValue)
                {
                    this.dtpRealStartTime.Checked = true;
                    this.dtpRealStartTime.Value = this.discipline.RealStartTime.Value;
                }
                else
                {
                    this.dtpRealStartTime.Checked = false;
                }

                // Real end time
                if (this.discipline.RealEndTime.HasValue)
                {
                    this.dtpRealEndTime.Checked = true;
                    this.dtpRealEndTime.Value = this.discipline.RealEndTime.Value;
                }
                else
                {
                    this.dtpRealEndTime.Checked = false;
                }

                // Wind
                if (this.discipline.Wind.HasValue)
                {
                    this.chkWind.Checked = true;
                    this.nudWind.Value = Convert.ToDecimal(this.discipline.Wind.Value);
                }
                else
                {
                    this.chkWind.Checked = false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = DatabaseHelper.GetDisciplineListById(discipline.ID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt.Rows[0]["Status"] = this.disciplinesStatusesComboBox.SelectedValue;
                    dt.Rows[0]["StartTime"] = this.dtpStartTime.Value;
                    
                    if (this.dtpRealStartTime.Checked)
                        dt.Rows[0]["RealStartTime"] = this.dtpRealStartTime.Value;
                    else
                        dt.Rows[0]["RealStartTime"] = DBNull.Value;

                    if (this.dtpRealEndTime.Checked)
                        dt.Rows[0]["RealEndTime"] = this.dtpRealEndTime.Value;
                    else
                        dt.Rows[0]["RealEndTime"] = DBNull.Value;

                    if (this.chkWind.Checked)
                        dt.Rows[0]["Wind"] = this.nudWind.Value;
                    else
                        dt.Rows[0]["Wind"] = DBNull.Value;

                    taDisciplineList.Update(dt.Rows[0]);
                }

                if (chkAll.Checked)
                {
                    Data.aDbDataSet.DisciplineListDataTable dtAll = DatabaseHelper.GetDisciplineListByCompetitionIdActive(discipline.CompetitionId);
                    foreach (DataRow dr in dtAll.Rows)
                    {
                        dr["RealStartTime"] = this.dtpRealStartTime.Value;
                    }
                    taDisciplineList.Update(dtAll);
                }

                this.Close();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkWind_CheckedChanged(object sender, EventArgs e)
        {
            this.nudWind.Enabled = this.chkWind.Checked;
        }

    }
}
